<?php
/**
 * Created by jinqiang on 2024/6/6 10:13.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Banshi;


use App\Http\Models\Mysql\SsyNewProductBaseCodeTable;

class BaseCodeService
{
    public function search($param)
    {
        $conn = (new SsyNewProductBaseCodeTable())->where('base_status', (Int)$param['status']);
        if($param['title'] ?? ""){
            $conn = $conn->where('base_category', 'like', "%".$param['title']."%");
        }
        $res = $conn->orderBy("createtime", "desc")->paginate((Int)$param['limit'])->toArray();
        foreach ($res['data'] as &$item){
            $item['base_size_code'] = unserialize($item['base_size_code']);
            $item['base_code'] = unserialize($item['base_code']);
            $item['sample_code'] = unserialize($item['sample_code']);
        }
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function setStatus($id, $status)
    {
        $data = ['base_status' => (Int)$status];
        return (new SsyNewProductBaseCodeTable())->updateData($id, $data);
    }

    public function setData($id = "", $data)
    {
        $baseCode = [];
        $sampleCode = $data['base_size_code'][0];
        foreach ($data['base_size_code'] as $item) {
            if($item['is_base']){
                //基码
                $baseCode = [
                    "size_code" => $item['size_code'],
                    "height_code" => $item['height_code'],
                    "waist_code" => $item['waist_code']
                ];
            }
            //样衣尺码
            $sampleCodeMaxHeight = (Int)($sampleCode['height_code']['title'] ?? 0);
            $height = (Int)($item['height_code']['title'] ?? 0);
            if($sampleCodeMaxHeight < $height){
                $sampleCode = $item;
            }
        }
        if(empty($baseCode)){
            return ['code' => -1, 'msg' => '请设置基码'];
        }
        $saveData = [
            "base_category" => $data['base_category'],
            "base_size_code" => serialize($data['base_size_code']),
            "base_code" => serialize($baseCode),
            "sample_code" => serialize([$sampleCode]),
            "type" => $data['type'],
            "createtime"=>date("Y-m-d H:i:s",time())
        ];
        if(empty($id)){
            $exist = (new SsyNewProductBaseCodeTable())->where('base_category', $data['base_category'])->exists();
            if($exist){
                return ['code' => -1, 'msg' => '服装类别名称已存在'];
            }
            return (new SsyNewProductBaseCodeTable())->createData($saveData);
        }else{
            $exist = (new SsyNewProductBaseCodeTable())->where('id', '<>', $id)->where('base_category', $data['base_category'])->exists();
            if($exist){
                return ['code' => -1, 'msg' => '服装类别名称已存在'];
            }
            unset($saveData['createtime']);
            return (new SsyNewProductBaseCodeTable())->updateData($id, $saveData);
        }
    }
}
